#import "ApplicationState.h"

@implementation ApplicationState

- (id)init
{
	if(!self) {
		self = [super init];
	}
	stateDict = [[NSUserDefaults standardUserDefaults] retain];
	if (![stateDict stringForKey:@"Chord"]) [stateDict setObject:@"0" forKey:@"Chord"];
	if (![stateDict stringForKey:@"Mode"]) [stateDict setObject:@"0" forKey:@"Mode"];
	if (![stateDict stringForKey:@"Position"]) [stateDict setObject:@"0" forKey:@"Position"];
	if (![stateDict stringForKey:@"Notation"]) [stateDict setObject:@"" forKey:@"Notation"];
	return self;
}

- (void)setChordState:(NSString *)myChord
{
	[stateDict setObject:myChord forKey:@"Chord"];
}

- (void)setModeState:(NSString *)myMode
{
	[stateDict setObject:myMode forKey:@"Mode"];
}

- (void)setPositionState:(NSString *)myPosition
{
	[stateDict setObject:myPosition forKey:@"Position"];
}

- (void)setNotationState:(NSString *)myNotation
{
	[stateDict setObject:myNotation forKey:@"Notation"];
}

- (void)setPreferredState:(NSString *)myPreferred forChordType:(NSString *)chordType
{
	chordType = [chordType copy];
	myPreferred = [myPreferred copy];
	NSLog([NSString stringWithFormat:@"PREFERITO : <%@:%@>",chordType,myPreferred]);
	[stateDict setObject:myPreferred forKey:chordType];
}

- (void)flushApplicationState
{
	[stateDict synchronize];
	[stateDict release];
	// [stateDict writeToFile:[@"~/Library/Preferences//com.aeroTeam.chords.plist" stringByExpandingTildeInPath] atomically:YES];
}

- (NSString *)getChordState
{
	return [stateDict objectForKey:@"Chord"];
}

- (NSString *)getModeState
{
	return [stateDict objectForKey:@"Mode"];
}

- (NSString *)getPositionState
{
	return [stateDict objectForKey:@"Position"];
}

- (NSString *)getNotationState
{
	return [stateDict objectForKey:@"Notation"];
}

- (NSString *)getPreferredState:(NSString *)chordType
{
	return ([stateDict objectForKey:chordType]==nil ? @"0" : [stateDict objectForKey:chordType]);
}

@end